import { defineStore } from 'pinia'
import { reqLogin, reqUserInfo } from '@/api/user';

import type { loginForm, loginResponseData } from '@/api/user/type'
import type { UserState } from './types/type'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token'
// 引用路由
import { constantRoute } from '@/router/routes'
let useUserStore = defineStore('User', {
    state: (): UserState => {
        return {
            // token: localStorage.getItem('TOKEN'),//用户唯一标识
            token: GET_TOKEN()!,
            menuRoutes: constantRoute,
            username: '',
            avatar: ''
        }
    },
    actions: {
        //用户登陆方法
        async userLogin(data: loginForm) {
            let result: loginResponseData = await reqLogin(data);
            // console.log(result);
            if (result.code == 200) {
                this.token = result.data.token;
                // console.log(this.token);
                // localStorage.setItem('TOKEN', (result.data.token as string))
                SET_TOKEN((result.data.token as string))
                return 'ok';
            } else {
                return Promise.reject(new Error(result.data.message))
            }
        },
        //获取用户信息
        async userInfo() {
            let result = await reqUserInfo()
            if (result.code == 200) {
                this.username = result.data.checkUser.username;
                this.avatar = result.data.checkUser.avatar;
            } else {

            }
        }
    },
    getters: {

    }
})

export default useUserStore;